import { ref } from 'vue';
import { useStorage } from '@/utils/storeage';

export default () => {
	const storage = useStorage();
	const USERINFO = ref(null);

	const setUserInfo = (user) => {
		USERINFO.value = user;
		storage.set('USERINFO', user);
	};

	const getToken = () => USERINFO.value?.token;

	const clearUserInfo = () => {
		USERINFO.value = null;
		storage.remove('USERINFO');
	};

	const initUserInfo = () => {
		const user = storage.get('USERINFO');
		if (user) {
			setUserInfo(user);
		}
	};

	const isLogin = () => {
		const token = USERINFO.value?.token;
		return !!token;
	};

	return {
		USERINFO,
		setUserInfo,
		getToken,
		clearUserInfo,
		initUserInfo,
		isLogin,
	};
};

